package com.newbingo.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import com.newbingo.dao.imple.IBettingCellDao;
import com.newbingo.dao.imple.IBettingItemDao;
import com.newbingo.dao.imple.IMemberDao;
import com.newbingo.dao.imple.IPeriodGameDao;
import com.newbingo.entity.BetSetting;
import com.newbingo.entity.BettingCell;
import com.newbingo.entity.BettingItem;
import com.newbingo.entity.Member;
import com.newbingo.entity.PeriodGame;
import com.newbingo.reportEntity.MemberReport;
import com.newbingo.service.imple.IBettingItemService;
import com.newbingo.util.Pager;
import com.newbingo.util.TimeUtil;

public class BettingItemService implements IBettingItemService {
	private IBettingItemDao bettingItemDao;
	private IMemberDao memberDao;
	private IBettingCellDao bettingCellDao;
	private IPeriodGameDao periodGameDao;

	/*
	 * 创建投注项
	 */
	public void createBettingItem(BettingItem bettingItem) {
		bettingItemDao.save(bettingItem);
	}

	/*
	 * 根据会员提交的筹码、投注格ID、和期数进行保存
	 */
	public void createBettingItem(Integer[] amount,Integer cellId[],Integer memberId,Integer periodGameId){
		for(int i=0;i<amount.length;i++){
			//若筹码和投注格ID都为0，则页面上在这个投注格会员未投注
			if(amount[i]!=null && cellId[i]!=null){
				//根据ID查询出持久化的会员，投注格和本期游戏
				Member member=memberDao.findById(memberId);
				BettingCell bettingCell=bettingCellDao.findById(cellId[i]);
				PeriodGame periodGame=periodGameDao.findById(periodGameId);
				//获取本期游戏的已经投注的信息
				//定义本期已经投注的筹码变量，根据查询出的已经投注的信息获取已经投注的筹码量
				List<BettingItem> bettingItems=bettingItemDao.findBettingItemsByPeriodGame(periodGameId);
				int hadBet=0;
				if(bettingItems!=null && bettingItems.size()>0){
					for(BettingItem bettingItem:bettingItems){
						hadBet+=bettingItem.getAmount();
					}
				}
				//获取此投注会员的代理商投注格设置(BetSetting)的Set集合，根据投注格ID循环获取出此投注格
				//获取此投注格的最大投注设置
				Set<BetSetting> betSettings=member.getAgent().getSettings();
				int maxBet=0;
				for(BetSetting betSetting:betSettings){
					if(betSetting.getBettingCell().equals(bettingCell)){
						maxBet=betSetting.getMaxBet();
					}
				}
				//若已经投注的筹码和现在投注的筹码和<=代理设置的最大投注，则新增一条投注,否则不添加
				if(amount[i]+hadBet<=maxBet){
					//若投注的分值小于或者等于当前账户余额分值，则进行投注操作
					if(amount[i]<=member.getAccount().getAmount()){
						BettingItem item=new BettingItem();
						item.setAmount(amount[i]);
						item.setMember(member);
						item.setBetTime(new Date());
						item.setBettingCell(bettingCell);
						item.setPeriodGame(periodGame);
						bettingItemDao.save(item);
						member.getAccount().setAmount(member.getAccount().getAmount()-amount[i]);
						memberDao.merger(member);
					}
				}
			}
		}
	}
	
	

	/*
	 * 删除投注项
	 */
	public void deleteBettingItem(BettingItem bettingItem) {
		bettingItemDao.delete(bettingItem);
	}

	/*
	 * 修改投注项
	 */
	public void updateBettingItem(BettingItem bettingItem) {
		bettingItemDao.merge(bettingItem);
	}

	/*
	 * 分页查询
	 */
	public Pager findByPage(int start, int limit, Integer pgno, Date startTime,
			Date endTime) {
		return bettingItemDao
				.findByPage(start, limit, pgno, startTime, endTime);
	}
	
	/*
	 * 查询出在时间段、游戏 的会员投注记录
	 */
	public List<BettingItem> findBettingItemsByPeriodTime(Date start,Date end,Integer memberId,Integer gameId){
		return bettingItemDao.findBettingItemsByPeriodTime(start, end, memberId,gameId);
	}
	
	/*
	 * 查询代理商报表
	 */
	public List findAgentReportByTime(Integer agentId,Integer dateNum){
		return bettingItemDao.findAgentReportByAgent(agentId, TimeUtil.getTimePeriod(dateNum));
	}
	

	public IBettingItemDao getBettingItemDao() {
		return bettingItemDao;
	}

	public void setBettingItemDao(IBettingItemDao bettingItemDao) {
		this.bettingItemDao = bettingItemDao;
	}

	public IMemberDao getMemberDao() {
		return memberDao;
	}

	public void setMemberDao(IMemberDao memberDao) {
		this.memberDao = memberDao;
	}

	public IBettingCellDao getBettingCellDao() {
		return bettingCellDao;
	}

	public void setBettingCellDao(IBettingCellDao bettingCellDao) {
		this.bettingCellDao = bettingCellDao;
	}
	public IPeriodGameDao getPeriodGameDao() {
		return periodGameDao;
	}

	public void setPeriodGameDao(IPeriodGameDao periodGameDao) {
		this.periodGameDao = periodGameDao;
	}
}
